歡迎來到 C++ 的起點。每一段通往這門強大語言的旅程,都從一個神聖的起點開始: main 函數。可以把它想像成高安全級別保險庫的大門。 作業系統透過呼叫 main 函數來執行一個 C++ 程式它不在乎內部有多少房間或寶藏,它只知道如何轉動這扇特定門的鑰匙,以啟動程式執行。
1. 入門之門的結構解析
這個「入門之門」不僅僅是一個名稱,更是一份正式的契約。為了讓作業系統滿意,你必須提供一個特定的函數簽名:一個 回傳類型 ( 內建類型int),以及 函數名稱和一個 參數清單 (以 ()表示)。邏輯本身則位於 函數本體中,這是一個 陳述句區塊 由 花括號{ }所保護。
2. 終止邏輯
這個 return 0; 陳述是最終的動作。它將一個值傳回環境,以標示成功,展現了 函數 如何使用 類型 來傳遞狀態。C++ 是格式無關的,意思是 int main() { return 0; } 與擴展形式的程式碼同樣有效。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
[Writing Task] Write a program to print Hello, World on the standard output.
int main() { std::cout << "Hello, World" << std::endl; return 0; }void main() { print("Hello World"); }main() { cout << Hello World; }int main { return "Hello World"; }✅ Correct!
Excellent! You defined the main function, used the standard output stream, and returned a success status.❌ Incorrect
Remember to include the return type 'int', the function body braces, and the proper output syntax.QUESTION 2
[Writing Task] We wrote the output in one large statement. Rewrite the program to use a separate statement to print each operand.
Use a single
std::cout with multiple << operators.Break the output into multiple
std::cout statements, each ending with a semicolon.Use a
printf for each word.Combine them all into a string variable first.
✅ Correct!
Correct. Each statement must end with a semicolon. Example: std::cout << "Hello, "; std::cout << "World";❌ Incorrect
To use separate statements, each std::cout call must be its own complete statement ending in a semicolon.QUESTION 3
What happens if you change the program to
return -1;?The program crashes immediately.
The operating system treats it as a failure indicator.
The compiler refuses to build the code.
It returns 1 to the user interface.
✅ Correct!
Yes. While the code is valid, most systems interpret non-zero returns from main as errors.❌ Incorrect
C++ allows non-zero returns, but they are conventionally used to signal that something went wrong.QUESTION 4
What is the mandatory name of the function where C++ execution begins?
Start()begin()main()init()✅ Correct!
Exactly. The OS specifically looks for the symbol 'main' to start your program.❌ Incorrect
Unlike some other languages, C++ strictly requires the name 'main' for its entry point.QUESTION 5
Which of these represents the 'Boundary of Logic' in a function?
Parentheses
( )Semicolons
;Curly braces
{ }Square brackets
[ ]✅ Correct!
Correct! The curly braces define the block of statements that make up the function body.❌ Incorrect
Parentheses are for parameters; curly braces define the functional container.Case Study: The OS-Program Contract
Analyzing Entry and Exit Points
You are debugging a program that compiles but does not show any error messages, yet the automated build system marks it as 'Failed'. You notice the main function ends with 'return 1;'.
Q
Explain why the build system marks the program as 'Failed' despite it running correctly.
Solution:
The build system checks the return value of the process. In C++, returning a non-zero value (like 1) from the
The build system checks the return value of the process. In C++, returning a non-zero value (like 1) from the
main function signals to the Operating System that the program encountered an error or did not complete successfully.Q
Rewrite the return statement to indicate 'Mission Success'.
Solution:
Returning zero is the universal signal for successful execution in C++ applications.
return 0;Returning zero is the universal signal for successful execution in C++ applications.
Q
If you needed to print 'Hello' and 'World' on separate lines using separate statements, how would you write the function body?
Solution:
{
std::cout << "Hello" << std::endl;
std::cout << "World" << std::endl;
return 0;
}